# pylint: disable-msg=E1101
"""
@author: Sergiy Tsymbalyuk
"""

import logging

from pylons import tmpl_context as c
from pylons.i18n import lazy_ugettext as _
from pylons.controllers.util import abort

from repoze.what.plugins.pylonshq import ControllerProtector
from repoze.what.predicates import Any, All, in_group

from hrms.lib.base import BaseController, render, Session
from hrms.lib.predicates import from_district
from hrms.model.person import Employee
from hrms.model.organization import Appointment

from chowa.lib.site.navigation import Tab
from chowa.model.organization import *

log = logging.getLogger(__name__)

@ControllerProtector(Any(All(in_group('hrms.hr'), from_district()),
                         in_group('hrms.administrators')))
class DistrictController(BaseController):
    __secondary_tabs__ = [
        [
            Tab(caption=_('Employees|Present'),
                params=dict(action='employees')),
            Tab(caption=_('Employees|Former'),
                params=dict(action='former_employees')),]
    ]
    
    def employees(self, id):
        query = Session.query(Division)
        query = query.join(Employee)
        query = query.filter(Division.district_id == id)
        query = query.order_by(Division.name)
        
        c.divisions = query.all()
        c.district = Session.query(District).get(id)
        return render('/hrms/organization/employees.html')
    
    def former_employees(self, id):
        query = Session.query(Employee).join(Appointment).join(Division)
        query = query.filter(Employee.is_employee == False)
        query = query.filter(Division.district_id == id)
        c.employees = query.all()
        c.district = Session.query(District).get(id)
        return render('/hrms/organization/former_employees.html')
    
    def offices(self, id):
        c.district = Session.query(District).get(id)
        return render('/hrms/organization/offices.html')
    